package com.foreverwin.mes.lims.service.impl;

import com.foreverwin.mes.core.base.FrontPage;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.lims.mapper.InactiveTimeMapper;
import com.foreverwin.mes.lims.mapper.LotTankMapper;
import com.foreverwin.mes.lims.model.InactiveTime;
import com.foreverwin.mes.lims.model.ItemTank;
import com.foreverwin.mes.lims.mapper.ItemTankMapper;
import com.foreverwin.mes.lims.model.LotTank;
import com.foreverwin.mes.lims.service.ItemTankService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
/**
 * <p>
 * 储罐储料信息 服务实现类
 * </p>
 *
 * @author Max
 * @since 2020-03-11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ItemTankServiceImpl extends ServiceImpl<ItemTankMapper, ItemTank> implements ItemTankService {
    private static Logger logger = LoggerFactory.getLogger( ItemTankServiceImpl.class);

    @Autowired
    private ItemTankMapper itemTankMapper;
    @Autowired
    private InactiveTimeMapper inactiveTimeMapper;
    @Autowired
    private LotTankMapper lotTankMapper;

    @Override
    public Page<ItemTank> selectPage(FrontPage<ItemTank> frontPage, ItemTank itemTank) {
        EntityWrapper<ItemTank> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(itemTank);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<ItemTank> selectList(ItemTank itemTank) {
        EntityWrapper<ItemTank> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(itemTank);
        return super.selectList(queryWrapper);
    }

    @Override
    public int tankHighOrLow(String site, String tank, String item, String highOrLow, Date actionTime) {
        //查询批次的罐子是否有维护时间上下限设置
        InactiveTime inactiveTime = new InactiveTime();
        inactiveTime.setSite( site );
        inactiveTime.setResrce( tank );
        inactiveTime.setItem( item );
        int count=0;
        InactiveTime itime = inactiveTimeMapper.selectOne( inactiveTime );
        if ( itime == null ) {
            logger.error("罐子["+tank+"]未维护呆滞时间设置");
            return 0;
        }
        String zInactiveTimeBo = itime.getHandle();
        //查询当前是否有正在执行养豆或者养粉的罐子
        ItemTank itemTank = new ItemTank();
        itemTank.setzInactiveTimeBo( zInactiveTimeBo );
        ItemTank iTank = itemTankMapper.selectCurrentTank( itemTank );
        if ( iTank == null) {
            logger.error("罐子["+tank+"]当前未有批次进行入罐");
            return 0;
        }
        //highOrLow为1时满仓，0时为清仓
        if ( "1".equals( highOrLow ) ) {
            /**
             * 粉罐
             */
            if ( "2".equals( iTank.getTankByUse() ) ) {
                List<LotTank> lotTankList = lotTankMapper.selectLotInTank( iTank.getHandle() );
                if ( lotTankList != null && lotTankList.size()>0 ) {
                    LotTank lotTank = lotTankList.get( lotTankList.size()-1 );
                    iTank.setStartTime( lotTank.getCheckinTime() );
                    iTank.setTankStatus("2");
                    count =itemTankMapper.updateById( iTank );
                }
            }
        } else if ( "0".equals( highOrLow ) ) {
                //罐子内批次出罐，结束罐子
                List<LotTank> lotTankList = lotTankMapper.selectLotInTank( iTank.getHandle() );
                if ( lotTankList != null && lotTankList.size()>0 ) {
                    for (LotTank lt : lotTankList) {
                        lt.setCheckoutTime( actionTime );
                        lt.setLotStatus( "2" );
                        //获取自定义字段罐子清仓后用完的时间
                        lotTankMapper.updateById( lt );
                    }
                }
                //结束当前罐子
                iTank.setEndTime( actionTime );
                    count =itemTankMapper.updateById( iTank );
            }
            return count;
    }

    /**
     * 查询当前未来结束的罐子
     *
     * @param itemTank
     * @return
     */
    @Override
    public ItemTank selectCurrentTank(ItemTank itemTank) {
        return itemTankMapper.selectCurrentTank(itemTank);
    }

    /**
     * 查询当前所有未结束的罐子
     */
    @Override
    public List<ItemTank> selectCurrentAllTank() {
        return itemTankMapper.selectCurrentAllTank();
    }

    /**
     * 更新延迟呆滞时间
     *
     * @param itemTank
     * @return
     */
    @Override
    public int updateDelayOfItemTank(ItemTank itemTank) {
        return itemTankMapper.updateDelayOfItemTank( itemTank);
    }


}